# -*- coding: utf-8 -*-
from datetime import timedelta, datetime
from jms.dm import jms_dm__dm_cn_departure_transfer_timely_route_agg_dt
from time import time
from utils.operators.rich_sql_sensor import RichSqlSensor

label = f"dws_departure_transfer_timely_route_agg_{int(time())}"
timeout = timedelta(minutes=10).seconds
doris_jms_dws__dws_departure_transfer_timely_route_agg = RichSqlSensor(
    task_id='doris_jms_dws__dws_departure_transfer_timely_route_agg',
    email=['zhangqinglin@jtexpress.com', 'yl_bigdata@yl-scm.com'],
    pool='broker_load_pool',
    conn_id='doris',
    retries=0,
    pre_sql=f"""
             TRUNCATE TABLE jms_dws.dws_departure_transfer_timely_route_agg PARTITION (p{{{{ execution_date | date_add(-1) | cst_ds_nodash }}}},p{{{{ execution_date | date_add(-2) | cst_ds_nodash }}}});
             LOAD LABEL jms_dws.{label} (
                 DATA INFILE("hdfs://yl-bg-hdp/dw/hive/jms_dm.db/external/dm_cn_departure_transfer_timely_route_agg_dt/dt={{{{ execution_date | date_add(-1) | cst_ds }}}}/*") 
                 INTO TABLE dws_departure_transfer_timely_route_agg FORMAT AS 'PARQUET'
                  (start_code,start_name,order_source_code,order_source_name,center_type,start_agent_code,start_agent_name,send_account,route_lack_account,transfer_intime_account,transfer_intime_rate,without_arrival_account,error_send_account,opp_delay_account,send_delay_account,shipment_delay_account,stop_delay_account,all_line_transfer_intime_account,all_line_transfer_intime_rate,shipment_error_account,mage_region_name,mage_region_code,temporary_dispatch_account,reserve_route_account,epidemic_hoarding_account,store_count,express_type_code,express_type_name,reserved_seconds,start_provider_id,start_provider_desc)
                 SET (dt = DATE('{{{{ execution_date | date_add(-1) | cst_ds }}}}')),
                 DATA INFILE("hdfs://yl-bg-hdp/dw/hive/jms_dm.db/external/dm_cn_departure_transfer_timely_route_agg_dt/dt={{{{ execution_date | date_add(-2) | cst_ds }}}}/*") 
                 INTO TABLE dws_departure_transfer_timely_route_agg FORMAT AS 'PARQUET'
                  (start_code,start_name,order_source_code,order_source_name,center_type,start_agent_code,start_agent_name,send_account,route_lack_account,transfer_intime_account,transfer_intime_rate,without_arrival_account,error_send_account,opp_delay_account,send_delay_account,shipment_delay_account,stop_delay_account,all_line_transfer_intime_account,all_line_transfer_intime_rate,shipment_error_account,mage_region_name,mage_region_code,temporary_dispatch_account,reserve_route_account,epidemic_hoarding_account,store_count,express_type_code,express_type_name,reserved_seconds,start_provider_id,start_provider_desc)
                 SET (dt = DATE('{{{{ execution_date | date_add(-2) | cst_ds }}}}'))
                 )
             WITH BROKER '{{{{ var.json.doris_brokers | random_choice }}}}'
             PROPERTIES ('timeout'='{timeout}', 'max_filter_ratio'='0.0')""",
    poke_sql=f"SHOW LOAD FROM jms_dws WHERE label = '{label}' ORDER BY CreateTime DESC LIMIT 1",
    sql_on_kill=f"CANCEL LOAD FROM jms_dws WHERE LABEL = '{label}'",
    success=lambda r: r[2] == 'FINISHED',
    failure=lambda r: (r[2] is not None and r[2] == 'CANCELLED', str(r[7])),
    poke_interval=60,
    # execution_timeout=max(datetime.now().replace(hour=8, minute=25, second=0) - datetime.now(), timedelta(seconds=1)),
)

doris_jms_dws__dws_departure_transfer_timely_route_agg << jms_dm__dm_cn_departure_transfer_timely_route_agg_dt
